﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace CoTuong
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>

    public class QuanLyQuanCo : Microsoft.Xna.Framework.DrawableGameComponent
    {
        SpriteBatch spriteBatch;
        NetworkGame m_NetworkGame;
        public List<QuanCo> DanhSachQuanCo;
        public List<QuanCo> QuanCoRemoved;

        enum LoaiQuan { Do, Xanh };
        public static int[,] ktQuanCo = new int[10, 9];

        public static Vector2 mouseClickedQuanCo;

        public QuanLyQuanCo(Game game)
            : base(game)
        {
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);
            DanhSachQuanCo = new List<QuanCo>();
            QuanCoRemoved = new List<QuanCo>();
            for (int i = 0; i < 10; i++)
                for (int j = 0; j < 9; j++)
                    ktQuanCo[i, j] = 0;
            
            KhoiTaoQuanCo();
            // TODO: Construct any child components here
        }

        public QuanLyQuanCo(Game game, SaveData saveData)
            : base(game)
        {
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);
            DanhSachQuanCo = new List<QuanCo>();
            QuanCoRemoved = new List<QuanCo>();

            for (int i = 0; i < 10 * 9; i++)
                ktQuanCo[i / 9, i % 9] = saveData.ktQuanCo[i];
            
            LoadQuanCo(saveData);     
        }

        public QuanLyQuanCo(Game game, NetworkGame networkGame)
            : base(game)
        {
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);
            m_NetworkGame = networkGame;
            DanhSachQuanCo = new List<QuanCo>();
            QuanCoRemoved = new List<QuanCo>();
            for (int i = 0; i < 10; i++)
                for (int j = 0; j < 9; j++)
                    ktQuanCo[i, j] = 0;
            KhoiTaoQuanCo();
        }
        
        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            try
            {
                if (Game1.mouseClickedPosition.X > 0 || Game1.mouseClickedPosition.Y > 0)
                    mouseClickedQuanCo = Game1.mouseClickedPosition;
                int luotDiHienTai = VanCo.LuotDi;

                for (int i = 0; i < DanhSachQuanCo.Count; i++)
                {
                    QuanCo q = DanhSachQuanCo[i];
                    if (q.getQuan == luotDiHienTai)
                        q.Update(gameTime);
                    else
                        continue;

                    //Kiểm tra ăn quân cờ
                    for (int j = 0; j < DanhSachQuanCo.Count; j++)
                    {
                        if (i == j)
                            continue;
                        QuanCo p = DanhSachQuanCo[j];
                        if (p.getQuan != luotDiHienTai)
                        {
                            if (q.getCot == p.getCot && q.getDong == p.getDong)
                            {
                                if (p is QuanGeneral)
                                {
                                    string name = Gamer.SignedInGamers[PlayerIndex.One].Gamertag;
                                    if (luotDiHienTai == 0)
                                        HighScore.WinUp(name);
                                    else
                                        HighScore.LoseUp(name);
                                    Game1.isEndGame = true;

                                }

                                //Lưu quân cờ bị ăn
                                QuanCoRemoved.Add(p);

                                //đánh dấu quân cờ bị ăn trong UndoData
                                VanCo.undoData[UndoData.top].QuanCoRemoved = true;

                                //Xóa quân cờ bị ăn trong danh sách
                                DanhSachQuanCo.Remove(p);


                                break;
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Game1.errorMessage = e.Message;
            }
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.BackToFront, SaveStateMode.None);

            for (int i = 0; i < DanhSachQuanCo.Count; i++)
            {
                QuanCo q = DanhSachQuanCo[i];
                q.Draw(gameTime, spriteBatch);
            }

            spriteBatch.End();

            base.Draw(gameTime);
        }

        private void KhoiTaoQuanCo()
        {
            //Quân tốt đỏ
            DanhSachQuanCo.Add(new QuanTot(Game.Content.Load<Texture2D>(@"Images\Quan Do\Tot"),
                6, 0, (int)LoaiQuan.Do, false));
            QuanLyQuanCo.ktQuanCo[6, 0] = 1;

            DanhSachQuanCo.Add(new QuanTot(Game.Content.Load<Texture2D>(@"Images\Quan Do\Tot"),
                6, 2, (int)LoaiQuan.Do, false));
            QuanLyQuanCo.ktQuanCo[6, 2] = 1;

            DanhSachQuanCo.Add(new QuanTot(Game.Content.Load<Texture2D>(@"Images\Quan Do\Tot"),
                6, 4, (int)LoaiQuan.Do, false));
            QuanLyQuanCo.ktQuanCo[6, 4] = 1;

            DanhSachQuanCo.Add(new QuanTot(Game.Content.Load<Texture2D>(@"Images\Quan Do\Tot"),
                6, 6, (int)LoaiQuan.Do, false));
            QuanLyQuanCo.ktQuanCo[6, 6] = 1;

            DanhSachQuanCo.Add(new QuanTot(Game.Content.Load<Texture2D>(@"Images\Quan Do\Tot"),
                6, 8, (int)LoaiQuan.Do, false));
            QuanLyQuanCo.ktQuanCo[6, 8] = 1;

            //Quân Xe Đỏ
            DanhSachQuanCo.Add(new QuanXe(Game.Content.Load<Texture2D>(@"Images\Quan Do\Xe"),
                9, 0, (int)LoaiQuan.Do, false));
            QuanLyQuanCo.ktQuanCo[9, 0] = 1;
            DanhSachQuanCo.Add(new QuanXe(Game.Content.Load<Texture2D>(@"Images\Quan Do\Xe"),
                9, 8, (int)LoaiQuan.Do, false));
            QuanLyQuanCo.ktQuanCo[9, 8] = 1;

            //Quân Mã Đỏ
            DanhSachQuanCo.Add(new QuanMa(Game.Content.Load<Texture2D>(@"Images\Quan Do\Ma"),
                9, 1, (int)LoaiQuan.Do, false));
            QuanLyQuanCo.ktQuanCo[9, 1] = 1;
            DanhSachQuanCo.Add(new QuanMa(Game.Content.Load<Texture2D>(@"Images\Quan Do\Ma"),
                9, 7, (int)LoaiQuan.Do, false));
            QuanLyQuanCo.ktQuanCo[9, 7] = 1;

            //Quân Tượng Đỏ
            DanhSachQuanCo.Add(new QuanTuong(Game.Content.Load<Texture2D>(@"Images\Quan Do\Tuong"),
                9, 2, (int)LoaiQuan.Do, false));
            QuanLyQuanCo.ktQuanCo[9, 2] = 1;
            DanhSachQuanCo.Add(new QuanTuong(Game.Content.Load<Texture2D>(@"Images\Quan Do\Tuong"),
                9, 6, (int)LoaiQuan.Do, false));
            QuanLyQuanCo.ktQuanCo[9, 6] = 1;

            //Quân Pháo Đỏ
            DanhSachQuanCo.Add(new QuanPhao(Game.Content.Load<Texture2D>(@"Images\Quan Do\Phao"),
                7, 1, (int)LoaiQuan.Do, false));
            QuanLyQuanCo.ktQuanCo[7, 1] = 1;
            DanhSachQuanCo.Add(new QuanPhao(Game.Content.Load<Texture2D>(@"Images\Quan Do\Phao"),
                7, 7, (int)LoaiQuan.Do, false));
            QuanLyQuanCo.ktQuanCo[7, 7] = 1;

            //Quân Sỉ Đỏ
            DanhSachQuanCo.Add(new QuanSi(Game.Content.Load<Texture2D>(@"Images\Quan Do\Si"),
                9, 3, (int)LoaiQuan.Do, false));
            QuanLyQuanCo.ktQuanCo[9, 3] = 1;
            DanhSachQuanCo.Add(new QuanSi(Game.Content.Load<Texture2D>(@"Images\Quan Do\Si"),
                9, 5, (int)LoaiQuan.Do, false));
            QuanLyQuanCo.ktQuanCo[9, 5] = 1;

            //Quân Tướng Đỏ
            DanhSachQuanCo.Add(new QuanGeneral(Game.Content.Load<Texture2D>(@"Images\Quan Do\General"),
                9, 4, (int)LoaiQuan.Do, false));
            QuanLyQuanCo.ktQuanCo[9, 4] = 1;


            //***-------------------------Quân Xanh---------------------***\\

            //Quân Tốt Xanh
            DanhSachQuanCo.Add(new QuanTot(Game.Content.Load<Texture2D>(@"Images\Quan Xanh\Tot"),
                3, 8, (int)LoaiQuan.Xanh, false));
            QuanLyQuanCo.ktQuanCo[3, 8] = 2;

            DanhSachQuanCo.Add(new QuanTot(Game.Content.Load<Texture2D>(@"Images\Quan Xanh\Tot"),
                3, 6, (int)LoaiQuan.Xanh, false));
            QuanLyQuanCo.ktQuanCo[3, 6] = 2;

            DanhSachQuanCo.Add(new QuanTot(Game.Content.Load<Texture2D>(@"Images\Quan Xanh\Tot"),
                3, 4, (int)LoaiQuan.Xanh, false));
            QuanLyQuanCo.ktQuanCo[3, 4] = 2;

            DanhSachQuanCo.Add(new QuanTot(Game.Content.Load<Texture2D>(@"Images\Quan Xanh\Tot"),
                3, 2, (int)LoaiQuan.Xanh, false));
            QuanLyQuanCo.ktQuanCo[3, 2] = 2;

            DanhSachQuanCo.Add(new QuanTot(Game.Content.Load<Texture2D>(@"Images\Quan Xanh\Tot"),
                3, 0, (int)LoaiQuan.Xanh, false));
            QuanLyQuanCo.ktQuanCo[3, 0] = 2;

            //Quân Xe Xanh
            DanhSachQuanCo.Add(new QuanXe(Game.Content.Load<Texture2D>(@"Images\Quan Xanh\Xe"),
                0, 0, (int)LoaiQuan.Xanh, false));
            QuanLyQuanCo.ktQuanCo[0, 0] = 2;

            DanhSachQuanCo.Add(new QuanXe(Game.Content.Load<Texture2D>(@"Images\Quan Xanh\Xe"),
                0, 8, (int)LoaiQuan.Xanh, false));
            QuanLyQuanCo.ktQuanCo[0, 8] = 2;

            //Quân Mã Xanh
            DanhSachQuanCo.Add(new QuanMa(Game.Content.Load<Texture2D>(@"Images\Quan Xanh\Ma"),
                0, 1, (int)LoaiQuan.Xanh, false));
            QuanLyQuanCo.ktQuanCo[0, 1] = 2;
            DanhSachQuanCo.Add(new QuanMa(Game.Content.Load<Texture2D>(@"Images\Quan Xanh\Ma"),
                0, 7, (int)LoaiQuan.Xanh, false));
            QuanLyQuanCo.ktQuanCo[0, 7] = 2;

            //Quân Tượng Xanh
            DanhSachQuanCo.Add(new QuanTuong(Game.Content.Load<Texture2D>(@"Images\Quan Xanh\Tuong"),
                0, 2, (int)LoaiQuan.Xanh, false));
            QuanLyQuanCo.ktQuanCo[0, 2] = 2;
            DanhSachQuanCo.Add(new QuanTuong(Game.Content.Load<Texture2D>(@"Images\Quan Xanh\Tuong"),
                0, 6, (int)LoaiQuan.Xanh, false));
            QuanLyQuanCo.ktQuanCo[0, 6] = 2;

            //Quân Pháo Xanh
            DanhSachQuanCo.Add(new QuanPhao(Game.Content.Load<Texture2D>(@"Images\Quan Xanh\Phao"),
                2, 1, (int)LoaiQuan.Xanh, false));
            QuanLyQuanCo.ktQuanCo[2, 1] = 2;
            DanhSachQuanCo.Add(new QuanPhao(Game.Content.Load<Texture2D>(@"Images\Quan Xanh\Phao"),
                2, 7, (int)LoaiQuan.Xanh, false));
            QuanLyQuanCo.ktQuanCo[2, 7] = 2;

            //Quân Sỉ Xanh
            DanhSachQuanCo.Add(new QuanSi(Game.Content.Load<Texture2D>(@"Images\Quan Xanh\Si"),
                0, 3, (int)LoaiQuan.Xanh, false));
            QuanLyQuanCo.ktQuanCo[0, 3] = 2;
            DanhSachQuanCo.Add(new QuanSi(Game.Content.Load<Texture2D>(@"Images\Quan Xanh\Si"),
                0, 5, (int)LoaiQuan.Xanh, false));
            QuanLyQuanCo.ktQuanCo[0, 5] = 2;

            //Quân Tướng Xanh
            DanhSachQuanCo.Add(new QuanGeneral(Game.Content.Load<Texture2D>(@"Images\Quan Xanh\General"),
                0, 4, (int)LoaiQuan.Xanh, false));
            QuanLyQuanCo.ktQuanCo[0, 4] = 2;
        }

        private void LoadQuanCo(SaveData saveData)
        {
            foreach (QuanCoData data in saveData.DSQuanCoData)
            {
                int cot = data.cot;
                int dong = data.dong;
                bool isSelected = false;
                Vector2 ViTriDiChuyen = data.ViTriDiChuyen;
                Vector2 ViTriQuanCo = data.ViTriQuanCo;
                int quan = data.quan;
                string s;
                if (quan == 0)
                    s = "Quan Do";
                else
                    s = "Quan Xanh";
                
                //1: Tot
                //2: Phao
                //3: Xe
                //4: Ma
                //5: Tuong
                //6: Si
                //7: General
                if (data.texture == 1)
                    DanhSachQuanCo.Add(new QuanTot(Game.Content.Load<Texture2D>(@"Images\" + s + @"\Tot"),
                        dong, cot, quan, isSelected));
                else if (data.texture == 2)
                    DanhSachQuanCo.Add(new QuanPhao(Game.Content.Load<Texture2D>(@"Images\" + s + @"\Phao"),
                        dong, cot, quan, isSelected));
                else if (data.texture == 3)
                    DanhSachQuanCo.Add(new QuanXe(Game.Content.Load<Texture2D>(@"Images\" + s + @"\Xe"),
                        dong, cot, quan, isSelected));
                else if (data.texture == 4)
                    DanhSachQuanCo.Add(new QuanMa(Game.Content.Load<Texture2D>(@"Images\" + s + @"\Ma"),
                        dong, cot, quan, isSelected));
                else if (data.texture == 5)
                    DanhSachQuanCo.Add(new QuanTuong(Game.Content.Load<Texture2D>(@"Images\" + s + @"\Tuong"),
                        dong, cot, quan, isSelected));
                else if (data.texture == 6)
                    DanhSachQuanCo.Add(new QuanSi(Game.Content.Load<Texture2D>(@"Images\" + s + @"\Si"),
                        dong, cot, quan, isSelected));
                else if (data.texture == 7)
                    DanhSachQuanCo.Add(new QuanGeneral(Game.Content.Load<Texture2D>(@"Images\" + s + @"\General"),
                        dong, cot, quan, isSelected));

                

            }
        }
    }
}